<template>
  <div class="app-container">
    <!-- 搜索模块 -->
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      
      <el-form-item label="日期" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" placeholder="选择日期" format="yyyy-MM-dd" value-format="yyyy-MM-dd" style="width: 240px" ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 图标权限模块 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>
    <!-- 展示模块 -->
    <el-table v-loading="loading" :data="ledgerList" stripe>
      <el-table-column prop="jhlx" label="计划类型" align="center"> </el-table-column>
      <el-table-column prop="tzhj" label="投资合计" align="center"> </el-table-column>
      <el-table-column label="东部" align="center">
      <el-table-column prop="dbtz" label="投资" align="center"> </el-table-column>
      <el-table-column prop="dbks" label="口数" align="center"> </el-table-column>
      <el-table-column prop="dbjc" label="进尺" align="center"> </el-table-column>
      </el-table-column>
      <el-table-column label="西部" align="center">
      <el-table-column prop="xbtz" label="投资" align="center"> </el-table-column>
      <el-table-column prop="xbks" label="口数" align="center"> </el-table-column>
      <el-table-column prop="xbjc" label="进尺" align="center"> </el-table-column>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    
  </div>
</template>
  
<script>
import { getWorkList,downloadLedger } from "@/api/vadmin/budget/ledger/ledgerapi";
import { Decrypt } from "@/utils/AesHmacMD5";

export default {
  name: "analywork",
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      valNames: [],
    
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 批复表格数据
      ledgerList: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,  // 当前页数
        pageSize: 10,  // 每页显示几个-------坨深斜3续
        year:'2024',createTime:''
      },
    };
  },
  created() {
    // 初始化数字台账列表
    debugger
    let currentDate = new Date();
    let year = currentDate.getFullYear();
    let month = currentDate.getMonth() + 1;  // 注意月份从0开始，需要加1
    let day = currentDate.getDate();
    this.queryParams.createTime=year + "-" + month + "-" + day;
    this.getList();
  },
  methods: {
    /** 查询数字台账列表 */
    getList() {
      this.loading = true;
      var _this=this
      getWorkList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        var resData = JSON.parse(Decrypt(response.data))
        this.ledgerList = resData.data;
        this.total = resData.count;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        scene: undefined,
        path: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //数据下载
    downloadData(){
      var ledgerids=[]
      for (var i = 0; i < this.ledgerList.length; i++) {
        ledgerids.push(this.ledgerList[i].id)
      }
      downloadLedger(this.addDateRange({ledgerids:ledgerids,pageSize:1000,pageNum:1}, this.dateRange)).then(response => {
        var resData = JSON.parse(Decrypt(response.data))
        var costList = resData.data;
        debugger
        var fileName = "data.csv"; //使用当前时间戳作为文件名
        var columnDelimiter = ","; //列分割符
        var lineDelimiter = "\r\n"; //行分割符
        var keyValue=resData.downloadStr
        let result = ""; // 最终结果的字符串
        for(var i=0;i<keyValue.length;i++){
          result +=this.transferred(keyValue[i]) +columnDelimiter;
        }
        result += lineDelimiter;
        for (var i = 0; i < costList.length; i++) {
          for(var j=0;j<keyValue.length;j++){
            console.log(costList[i][keyValue[j]])
            result +=this.transferred(costList[i][keyValue[j]]==undefined ? '': costList[i][keyValue[j]]+'') +columnDelimiter;
          }
          result += lineDelimiter;
        }
        this.downCsvFile(fileName,result)
        // this.total = resData.count;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$refs.uploadRef.clearFiles()
    },
    /** 文件下载 **/
    handleDownload(row) {
      this.download(row.path, row.name);
    },
    transferred(data) {
      return data.replace(/<br>/g, "\r\n").replace(/&gt;/g, ">");
    },
    downCsvFile(fileName, result) {
      var fileName = fileName; //使用当前时间戳作为文件名
      var blob = new Blob(["\uFEFF" + result], { type: "text/csv;" }); //记得将编码格式设置一下，避免最终下载的文件出现乱码
      var downloadLink = document.createElement("a");
      if ("download" in downloadLink) {
        var url = URL.createObjectURL(blob);
        downloadLink.href = url;
        downloadLink.download = fileName;
        downloadLink.hidden = true;
        document.body.appendChild(downloadLink);
        downloadLink.click();
        document.body.removeChild(downloadLink);
      } else {
        if (navigator.msSaveBlob) {
          //IE10+
          navigator.msSaveBlob(blob, fileName);
        }
      }
    },
  }
};
</script>
   <style scoped>
/* ::v-deep .el-table th.el-table__cell > .cell {
    display: inline-block;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    position: relative;
    vertical-align: middle;
    padding-left: 10px;
    padding-right: 10px;
    width: 100%;
    white-space: nowrap !important; */
/* } */

  </style>